package com.flqy.baselibrary.widget;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import android.text.TextUtils;
import android.util.AttributeSet;

import com.bumptech.glide.Glide;
import com.bumptech.glide.RequestBuilder;
import com.bumptech.glide.load.DataSource;
import com.bumptech.glide.load.Transformation;
import com.bumptech.glide.load.engine.GlideException;
import com.bumptech.glide.request.RequestListener;
import com.bumptech.glide.request.target.Target;
import com.flqy.baselibrary.R;
import com.flqy.baselibrary.widget.transform.CropCircleTransformation;

/**
 * @author lu
 */
public class RemoteImageView extends AppCompatImageView {

    private static final int NONE = -1;
    /**
     * 网络图片url
     */
    private String mUrl;

    private int mDefaultImageResource = NONE;
    private Drawable mDefaultDrawable;
    private Transformation<Bitmap> bitmapTransformation;
    private int mErrorImageResource = NONE;
    private Drawable mErrorDrawable;
    private OnErrorListener mOnErrorListener;
    private static final int CROP_CIRCLE = 1;

    public String getImageUri() {
        return mUrl;
    }

    public RemoteImageView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context, attrs);
    }


    public RemoteImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    public RemoteImageView(Context context) {
        super(context);
    }

    private void init(Context context, AttributeSet attrs) {
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.RemoteImageView);
        if (a != null) {
            if (a.hasValue(R.styleable.RemoteImageView_transformation)) {
                int transFormation = a.getInt(R.styleable.RemoteImageView_transformation, 0);
                switch (transFormation) {
                    case CROP_CIRCLE:
                        bitmapTransformation = new CropCircleTransformation(context);
                        break;
                    default:
                        break;
                }
            }

            a.recycle();
        }
    }


    /**
     * 加载网络图片
     *
     * @param url eg. http://yourwebsite.com/abz.jpg
     */
    public void setImageUri(final String url) {
        if (isActivityDestoryed())
            return;
        if (TextUtils.isEmpty(url)) {
            if (mDefaultDrawable != null) {
                setImageDrawable(mDefaultDrawable);
            } else if (mDefaultImageResource != NONE) {
                setImageResource(mDefaultImageResource);
            }
            return;
        }
        mUrl = url;
        RequestBuilder<Drawable> request = Glide.with(getContext()).load(url);
        if (mDefaultDrawable != null) {
            request.placeholder(mDefaultDrawable);
        } else if (mDefaultImageResource != NONE) {
            request.placeholder(mDefaultImageResource);
        }
        if (bitmapTransformation != null) {
            request.transform(bitmapTransformation);
        }
        if (mErrorDrawable != null) {
            request.error(mErrorDrawable);
        } else if (mErrorImageResource != NONE) {
            request.error(mErrorImageResource);
        }
        request.listener(new RequestListener<Drawable>() {
            @Override
            public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Drawable> target, boolean isFirstResource) {
                if (mOnErrorListener != null) {
                    mOnErrorListener.onLoadError(url);
                    return true;
                }
                return false;
            }

            @Override
            public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
                return false;
            }
        });
        request.into(this);
    }

    public interface OnErrorListener {
        public void onLoadError(String url);
    }

    public void setOnErrorListener(OnErrorListener onErrorListener) {
        this.mOnErrorListener = onErrorListener;
    }

    @SuppressLint("NewApi")
    private boolean isActivityDestoryed() {
        Context context = getContext();
        if (context instanceof Activity) {
            return ((Activity) context).isFinishing();
        }
        return false;
    }

    public void setDefaultImageDrawable(Drawable defaultDrawable) {
        this.mDefaultDrawable = defaultDrawable;
    }

    @SuppressWarnings("deprecation")
    public void setDefaultImageBitmap(Bitmap defaultImageBitmap) {
        this.mDefaultDrawable = new BitmapDrawable(defaultImageBitmap);
    }

    /**
     * 设置默认图片资源id
     *
     * @param resId
     */
    public void setDefaultImageResource(int resId) {
        mDefaultImageResource = resId;
    }

    public void setImageUri(int defResId, String url) {
        setDefaultImageResource(defResId);
        setImageUri(url);
    }

    public void setImageUri(Drawable defaultDrawable, String url) {
        if (mUrl == null || !this.mUrl.equals(url)) {
            setDefaultImageDrawable(defaultDrawable);
            setImageUri(url);
        }
    }

    public int getErrorImageResource() {
        return mErrorImageResource;
    }


    public void setErrorImageResource(int errorImageResource) {
        this.mErrorImageResource = errorImageResource;
    }

    public Drawable getErrorDrawable() {
        return mErrorDrawable;
    }

    public void setErrorDrawable(Drawable errorDrawable) {
        this.mErrorDrawable = errorDrawable;
    }

    public void setBitmapTransformation(Transformation<Bitmap> tf) {
        this.bitmapTransformation = tf;
    }
}
